package com.example.server.controller.admin;

import com.example.server.common.config.annotation.MustLogin;
import com.example.server.common.config.auth.TokenUser;
import com.example.server.common.constant.Result;
import com.example.server.service.admin.AdminCustomerService;
import com.example.server.vo.StaffVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

/**
 * @author china.fuyao@outlook.com
 * @date 2020-04-19 13:49
 */
@Api(tags = "Admin用户相关接口")
@RestController
@RequestMapping("/admin/customer")
public class AdminCustomerController {
    @Resource
    AdminCustomerService adminCustomerService;

    @ApiOperation(value = "登录", notes = "用户名密码登录")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户名", name = "name", required = true),
            @ApiImplicitParam(value = "密码", name = "password", required = true)
    })
    @PostMapping("/login")
    public Result login(String name, String password) {
        return adminCustomerService.login(name, password);
    }

    @MustLogin(isAdmin = true)
    @ApiOperation(value = "退出登录", notes = "退出登录")
    @PostMapping("/logout")
    public Result login(@ApiIgnore TokenUser tokenUser) {
        return adminCustomerService.logout(tokenUser);
    }

    @MustLogin(isAdmin = true)
    @ApiOperation(value = "员工列表", notes = "员工列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码", name = "page", required = true),
            @ApiImplicitParam(value = "数量", name = "limit", required = true),
            @ApiImplicitParam(value = "员工名称", name = "customerName", required = false),
            @ApiImplicitParam(value = "角色id", name = "roleId", required = false)
    })
    @GetMapping("/staff/list")
    public Result staffList(Integer page, Integer limit, String customerName, Integer roleId, @ApiIgnore TokenUser tokenUser) {
        return adminCustomerService.staffList(page, limit, customerName, roleId, tokenUser.getCustomerId());
    }

    @MustLogin(isAdmin = true)
    @ApiOperation(value = "新增员工", notes = "新增员工")
    @PostMapping("/staff/add")
    public Result addStaff(@RequestBody StaffVO staffVO, @ApiIgnore TokenUser tokenUser) {
        return adminCustomerService.addStaff(staffVO, tokenUser.getCustomerId());
    }

    @MustLogin(isAdmin = true)
    @ApiOperation(value = "修改员工", notes = "修改员工")
    @PostMapping("/staff/update")
    public Result updateStaff(@RequestBody StaffVO staffVO, @ApiIgnore TokenUser tokenUser) {
        return adminCustomerService.updateStaff(staffVO, tokenUser.getCustomerId());
    }

    @MustLogin(isAdmin = true)
    @ApiOperation(value = "修改员工密码", notes = "修改员工密码")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码", name = "oldpassword", required = true),
            @ApiImplicitParam(value = "数量", name = "newPwdone", required = true),
            @ApiImplicitParam(value = "员工名称", name = "newPwdTwo", required = false)
    })
    @PostMapping("/staff/modifyPw")
    public Result updateStaffPw(String oldpassword,String newPwdone,String newPwdTwo, @ApiIgnore TokenUser tokenUser) {
        return adminCustomerService.updateStaffPw(oldpassword,newPwdone,newPwdTwo, tokenUser.getCustomerId());
    }


    @MustLogin(isAdmin = true)
    @ApiOperation(value = "删除员工", notes = "删除员工")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码", name = "customerIds", required = true)
    })
    @PostMapping("/staff/del")
    public Result delStaff(String customerIds, @ApiIgnore TokenUser tokenUser) {
        return adminCustomerService.delStaff(customerIds, tokenUser.getCustomerId());
    }
    @MustLogin(isAdmin = true)
    @ApiOperation(value = "启/禁用员工", notes = "启/禁用员工")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "角色ID", name = "customerIds", required = true),
            @ApiImplicitParam(value = "启用/禁用状态", name = "enable", required = true)
    })
    @PostMapping("/staff/setEnable")
    public Result setEnable(String customerIds, Integer enable, @ApiIgnore TokenUser tokenUser) {
        return adminCustomerService.setEnable(customerIds, enable, tokenUser.getCustomerId());
    }
}
